import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testservice:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/service/1_get_service_list.yaml'))
    def test_get_service_list(self, serviceinfo):
        # 获取service列表接口-正常
        base_url = datas.baseUrl + Testservice.url_unify
        url = base_url + serviceinfo['request']['url'].replace('{namespace}', 'default')
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/service/2_create_service.yaml'))
    def test_create_service(self, serviceinfo):
        # 创建service接口-正常
        base_url = datas.baseUrl + Testservice.url_unify
        url = base_url + serviceinfo['request']['url'].replace('{namespace}', 'default')
        method = serviceinfo['request']['method']
        data = serviceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        servicename = req.json()['metadata']['name']
        globals()['servicename'] = servicename
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/service/3_get_specific_service.yaml'))
    def test_get_specific_service(self, serviceinfo):
        # 获取指定service接口-正常
        base_url = datas.baseUrl + Testservice.url_unify
        url = (base_url + serviceinfo['request']['url']
               .replace('{namespace}', 'default').replace('{servicename}', globals()['servicename']))
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/service/4_update_service.yaml'))
    def test_update_service(self, serviceinfo):
        # 更新指定service接口-正常
        base_url = datas.baseUrl + Testservice.url_unify
        url = (base_url + serviceinfo['request']['url']
               .replace('{namespace}', 'default').replace('{servicename}', globals()['servicename']))
        method = serviceinfo['request']['method']
        serviceinfo['request']['data'] = globals()['updatereqbody']
        data = serviceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/service/5_update_section_service.yaml'))
    def test_update_section_service(self, serviceinfo):
        # 部分更新指定service的接口-正常
        base_url = datas.baseUrl + Testservice.url_unify
        url = (base_url + serviceinfo['request']['url']
               .replace('{namespace}', 'default').replace('{servicename}', globals()['servicename']))
        method = serviceinfo['request']['method']
        data = serviceinfo['request']['data']
        header = serviceinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/service/6_delete_specific_service.yaml'))
    def test_delete_specific_service(self, serviceinfo):
        # 删除service接口-正常
        base_url = datas.baseUrl + Testservice.url_unify
        url = (base_url + serviceinfo['request']['url']
               .replace('{namespace}', 'default').replace('{servicename}', globals()['servicename']))
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/service/1_get_service_list_exception.yaml'))
    def test_get_service_list_exception(self, serviceinfo):
        # 获取service列表接口-异常
        base_url = datas.baseUrl + Testservice.url_unify
        url = base_url + serviceinfo['request']['url'].replace('{namespace}', 'default')
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/service/2_create_service_exception.yaml'))
    def test_create_service_exception(self, serviceinfo):
        # 创建service接口-异常
        base_url = datas.baseUrl + Testservice.url_unify
        url = base_url + serviceinfo['request']['url'].replace('{namespace}', 'default')
        method = serviceinfo['request']['method']
        data = serviceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/service/3_get_specific_service_exception.yaml'))
    def test_get_specific_service_exception(self, serviceinfo):
        # 获取指定service接口-异常
        base_url = datas.baseUrl + Testservice.url_unify
        url = (base_url + serviceinfo['request']['url']
               .replace('{namespace}', 'default').replace('{servicename}', globals()['servicename']))
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/service/4_update_service_exception.yaml'))
    def test_update_service_exception(self, serviceinfo):
        # 更新指定service接口-异常
        base_url = datas.baseUrl + Testservice.url_unify
        url = (base_url + serviceinfo['request']['url']
               .replace('{namespace}', 'default').replace('{servicename}', globals()['servicename']))
        method = serviceinfo['request']['method']
        serviceinfo['request']['data'] = globals()['updatereqbody']
        data = serviceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/service/5_update_section_service_exception.yaml'))
    def test_update_section_service_exception(self, serviceinfo):
        # 部分更新指定service的接口-异常
        base_url = datas.baseUrl + Testservice.url_unify
        url = (base_url + serviceinfo['request']['url']
               .replace('{namespace}', 'default').replace('{servicename}', globals()['servicename']))
        method = serviceinfo['request']['method']
        data = serviceinfo['request']['data']
        header = serviceinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.service
    @pytest.mark.parametrize('serviceinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/service/6_delete_specific_service_exception.yaml'))
    def test_delete_specific_service_exception(self, serviceinfo):
        # 删除service接口-异常
        base_url = datas.baseUrl + Testservice.url_unify
        url = (base_url + serviceinfo['request']['url']
               .replace('{namespace}', 'default').replace('{servicename}', globals()['servicename']))
        method = serviceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceinfo['validate'][0]['equals']['status_code']
